package com.homedo.microservice.open.service.gateway.manager.web.remote.api.impl;

import com.google.common.collect.Lists;
import com.homedo.common.bean.data.RequestData;
import com.homedo.common.bean.data.ResponseData;
import com.homedo.common.bean.vo.Order;
import com.homedo.common.bean.vo.Pagination;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.api.IRouteServiceServiceRemote;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteServiceAddAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteServicePageAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.req.RouteServiceUpdateAReq;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteServiceAResp;
import com.homedo.microservice.open.service.gateway.manager.web.api.bean.model.resp.RouteServicePageAResp;
import com.homedo.microservice.open.service.gateway.manager.web.convent.CommonConvent;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.RouteService;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.po.enhanced.RouteServiceEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.bean.query.RouteServiceQuery;
import com.homedo.microservice.open.service.gateway.manager.web.gateway.service.RouteServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;

@RestController
public class RouteServiceServiceRemoteImpl implements IRouteServiceServiceRemote {
    @Autowired
    private RouteServiceService routeServiceService;

    @Override
    public ResponseData<RouteServicePageAResp> list(RouteServicePageAReq routeServicePageAReq) {
        ResponseData<RouteServicePageAResp> responseData = new ResponseData<>();

        if (routeServicePageAReq.getPage() == 0) {
            routeServicePageAReq.setPage(1);
        }
        if (routeServicePageAReq.getSize() == 0) {
            routeServicePageAReq.setSize(Integer.MAX_VALUE);//默认查询全部
        }

        RouteServiceQuery routeServiceQuery = new RouteServiceQuery();

        routeServiceQuery.setApplicationNames(Lists.newArrayList(routeServicePageAReq.getApplicationName()));
        if (Detect.notEmpty(routeServicePageAReq.getLikeComment())) {
            routeServiceQuery.setLikeComment(URLDecoder.decode(routeServicePageAReq.getLikeComment()));
        }
        if (routeServicePageAReq.getStatus() != null) {
            routeServiceQuery.setStatuses(Lists.newArrayList(routeServicePageAReq.getStatus()));
        }
        routeServiceQuery.setLikeId(routeServicePageAReq.getLikeId());

        Pagination<RouteServiceEnhanced> pagination = new Pagination<>(routeServicePageAReq.getPage(), routeServicePageAReq.getSize(), true);
        pagination.addOrder(new Order(RouteService.COLUMN_STATUS, Order.ORDER_DESC), RouteService.class)
                .addOrder(new Order(RouteService.COLUMN_CREATE_TIME, Order.ORDER_DESC), RouteService.class);

        pagination = routeServiceService.getPagination(routeServiceQuery, pagination, null);

        RouteServicePageAResp routeServicePageAResp = new RouteServicePageAResp();
        routeServicePageAResp.setCount(pagination.getCount());
        routeServicePageAResp.setPage(pagination.getIndex());
        routeServicePageAResp.setSize(pagination.getSize());

        if (Detect.notEmpty(pagination.getItems())) {
            routeServiceService.appendRouteServiceUrlEnhanceds(pagination.getItems(), null);
            routeServiceService.appendAlertUserEnhanceds(pagination.getItems(), null);
            routeServicePageAResp.setItems(CommonConvent.convent2RouteServiceAResps(pagination.getItems()));
        }

        responseData.setData(routeServicePageAResp);

        return responseData;
    }

    @Override
    public ResponseData<RouteServiceAResp> detail(@PathVariable("id") String id) {
        ResponseData<RouteServiceAResp> responseData = new ResponseData<>();

        RouteServiceEnhanced routeServiceEnhanced = routeServiceService.getEnhanced(id, null);

        routeServiceService.appendRouteServiceUrlEnhanceds(routeServiceEnhanced, null);
        routeServiceService.appendAlertUserEnhanceds(routeServiceEnhanced, null);

        responseData.setData(CommonConvent.convent2RouteServiceAResp(routeServiceEnhanced));

        return responseData;
    }

    @Override
    public ResponseData update(@RequestBody RequestData<RouteServiceUpdateAReq> requestData) {
        ResponseData responseData = new ResponseData();

        RouteServiceUpdateAReq routeServiceUpdateAReq = requestData.getRequest();

        RouteService routeService = routeServiceService.updateRouteService(routeServiceUpdateAReq.getId(), routeServiceUpdateAReq.getHealthUrl(), routeServiceUpdateAReq.getComment(), routeServiceUpdateAReq.getStatus(), null);

        return responseData;
    }

    @Override
    public ResponseData<RouteServiceAResp> add(@RequestBody RequestData<RouteServiceAddAReq> requestData) {
        ResponseData<RouteServiceAResp> responseData = new ResponseData<>();

        RouteService routeService = new RouteService();

        BeanUtils.copyProperties(requestData.getRequest(), routeService);

        routeService = routeServiceService.addRouteService(routeService, null);

        responseData.setData(CommonConvent.convent2RouteServiceAResp(routeServiceService.convent2Enhanced(routeService)));

        return responseData;
    }

    @Override
    public ResponseData delete(@PathVariable("id") String id) {
        ResponseData responseData = new ResponseData();

        routeServiceService.deleteRouteService(id, null);

        return responseData;
    }
}
